﻿using System;
using System.Collections.Specialized;
using System.ComponentModel;
using Notoric.Commanding;
using Notoric.Gui.Windows;

namespace Notoric.Gui.Commands
{
    /// <summary>
    /// Implements a command for deleting selected section(s) on a currently active page.
    /// </summary>
    public class DeleteSectionCommand : RibbonCommand
    {
        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="DeleteSectionCommand"/> class.
        /// </summary>
        /// <param name="mainWindow">
        /// <para>
        /// Type: <see cref="MainWindow"/>
        /// </para>
        /// <para>
        /// The main application window.
        /// </para>
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// The <paramref name="mainWindow"/> is <see langword="null"/>.
        /// </exception>
        public DeleteSectionCommand(MainWindow mainWindow)
            : base(mainWindow)
        {
            mainWindow.PropertyChanged += new PropertyChangedEventHandler(mainWindow_PropertyChanged);
        }

        #endregion

        #region ICommand Members

        /// <inheritdoc/>
        public override bool CanExecute(object parameter)
        {
            return ApplicationWindow.ActivePage != null && ApplicationWindow.ActivePage.SelectedSections.Count > 0;
        }

        /// <inheritdoc/>
        public override void Execute(object parameter)
        {
            var macro = new UndoableMacroCommand(ApplicationWindow.ActivePage.UndoStack, ApplicationWindow.ActivePage.RedoStack);
            if (ApplicationWindow.ActivePage.SelectedSections.Count > 1)
                macro.Name = "Delete Sections";
            else
                macro.Name = "Delete Section";

            foreach (var section in ApplicationWindow.ActivePage.SelectedSections)
            {
                macro.Commands.Add(
                    new ViewModel.DeleteSectionCommand(ApplicationWindow.ActivePage.UndoStack, ApplicationWindow.ActivePage.RedoStack)
                    {
                        Section = section
                    }
                );
            }

            ApplicationWindow.ActivePage.ClearSelection();
            macro.Execute(null);
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Handles the PropertyChanged event of the mainWindow control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.ComponentModel.PropertyChangedEventArgs"/> instance containing the event data.</param>
        private void mainWindow_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "ActivePage")
            {
                if (activePage != null)
                    activePage.SelectedSections.CollectionChanged -= SelectedSections_CollectionChanged;

                activePage = ApplicationWindow.ActivePage;

                if (activePage != null)
                    activePage.SelectedSections.CollectionChanged += new NotifyCollectionChangedEventHandler(SelectedSections_CollectionChanged);

                RaiseCanExecuteChanged();
            }
        }

        /// <summary>
        /// Handles the CollectionChanged event of the SelectedSections collection of the currently active page.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Collections.Specialized.NotifyCollectionChangedEventArgs"/> instance containing the event data.</param>
        private void SelectedSections_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            RaiseCanExecuteChanged();
        }

        #endregion

        #region Private Fields

        /// <summary>
        /// Holds the currently active page.
        /// </summary>
        private ViewModel.Elements.Page activePage;

        #endregion
    }
}
